import org.apache.commons.lang.time.DateUtils;

import org.springframework.security.context.SecurityContextHolder as SCH

class NotasController {

    def scaffold = Notas
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ notasInstanceList: Notas.list( params ), notasInstanceTotal: Notas.count() ]
    }

    def painelDia = {

        def notasDia = Notas.findAllByData(DateUtils.truncate(new Date(), Calendar.DATE))
        def soma = 0;
        def qtd = 0;
        notasDia.each {
            println(it)
            soma += it.nota
            qtd++
        }
        def mediaDia = 0
        if (qtd!=0) {
          mediaDia = soma / qtd
        }
        return [mediaDia: mediaDia]
    }

    def show = {
        def notasInstance = Notas.get( params.id )

        if(!notasInstance) {
            flash.message = "Notas not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ notasInstance : notasInstance ] }
    }

    def delete = {
        def notasInstance = Notas.get( params.id )
        if(notasInstance) {
            try {
                notasInstance.delete(flush:true)
                flash.message = "Notas ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Notas ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Notas not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def notasInstance = Notas.get( params.id )

        if(!notasInstance) {
            flash.message = "Notas not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ notasInstance : notasInstance ]
        }
    }

    def update = {
        def notasInstance = Notas.get( params.id )
        if(notasInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(notasInstance.version > version) {
                    
                    notasInstance.errors.rejectValue("version", "notas.optimistic.locking.failure", "Another user has updated this Notas while you were editing.")
                    render(view:'edit',model:[notasInstance:notasInstance])
                    return
                }
            }
            notasInstance.properties = params
            if(!notasInstance.hasErrors() && notasInstance.save()) {
                flash.message = "Notas ${params.id} updated"
                redirect(action:show,id:notasInstance.id)
            }
            else {
                render(view:'edit',model:[notasInstance:notasInstance])
            }
        }
        else {
            flash.message = "Notas not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def notaHoje = Notas.findAllByData(DateUtils.truncate(new Date(), Calendar.DATE))
        if (notaHoje) {
            flash.message = "A nota de hoje já foi informada"
            redirect(action:show, id : notaHoje.id[0])
        }
        else {
            def notasInstance = new Notas()
            notasInstance.properties = params
            return ['notasInstance':notasInstance]
        }
    }

    def save = {
        def notasInstance = new Notas(params)
        notasInstance.data = new Date();
        //todo: descomentar
        notasInstance.usuario = User.findByUsername(SCH.context.authentication.principal.username)
        if(!notasInstance.hasErrors() && notasInstance.save()) {
            flash.message = "Notas ${notasInstance.id} created"
            redirect(action:show,id:notasInstance.id)
        }
        else {
            render(view:'create',model:[notasInstance:notasInstance])
        }
    }
}
